package gui;

import java.awt.Component;
import java.awt.Dimension;

import javax.swing.BoxLayout;
import javax.swing.JPanel;

import simulation.initiatives.AbstractEngine;

import common.Constants;
import common.IGrid;
import common.SimulationSettings;

public class EarthPanel extends JPanel {

  private static final long serialVersionUID = -1108120537851962997L;  
  private SunDisplay sunDisplay;
  private EarthGrid earthGrid;
  
  public EarthPanel(SimulationSettings settings) {
    super();
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    setMinimumSize(new Dimension(20,20));
    setMaximumSize(new Dimension(Constants.FRAME_WIDTH,Constants.FRAME_HEIGHT));
    setPreferredSize(new Dimension(20, 20));
    
    if(settings.isPresentationThreaded()) {
      Thread t;
      if(settings.getInitiative().equals(Constants.INITIATIVE_PULL)) {
        earthGrid = new EarthGridThreadPull(settings.getEarthImage());
        t = new Thread(earthGrid);
        t.setName("PresThread-Pulling");
      }
      else {
        earthGrid = new EarthGridThreadPush(settings.getEarthImage());
        t = new Thread(earthGrid);
        t.setName("PresThread-Pushing");
      }
      t.start();
    }
    else {
      earthGrid = new EarthGrid(settings.getEarthImage());
    }
    
    earthGrid.setAlignmentX(Component.LEFT_ALIGNMENT);
    
//    settings.setRadius(getRadius());
    
    sunDisplay = new SunDisplay(earthGrid.getWidth());
    sunDisplay.setAlignmentX(Component.LEFT_ALIGNMENT);
    
    add(sunDisplay);
    add(earthGrid);
  }
  
  public void drawGrid(IGrid grid) {
    earthGrid.setGridSize(grid);
    sunDisplay.drawSunPath(earthGrid.getWidth());
    repaint();
  }
  
//  public int getRadius() {
//    return earthGrid.getRadius();
//  }
  
  public void updateGrid(AbstractEngine engine) {
    earthGrid.updateGrid(engine);
  }
  
  public void updateGrid(IGrid grid) {
    earthGrid.updateGrid(grid);
  }
  
  public void moveSunPosition(float degrees) {
    sunDisplay.moveSunPosition(degrees);
    repaint();
  }
  
  public void reset() {
    sunDisplay.reset();
    earthGrid.reset();
    repaint();
  }
  
  public void setMapOpacity(float value) {
    earthGrid.setMapOpacity(value);
    repaint();
  }
  
  public void enableTooltips() {
    earthGrid.enableTooltips();
  }
  
  public void disableTooltips() {
    earthGrid.disableTooltips();
  }

}
